import AppNode from "../core/app_node";
import data from "../core/cache_data";
import Prefab from "../core/prefab";
import { RegClass } from "../core/serialize";
import Utils, { Random } from "../core/utils";
import BoxSettings from "./box_settings";
import Client from "./client";
@RegClass("PageHome")
export default class PageHome extends AppNode {
    clientWrap: HTMLDivElement = null;
    clients: Client[] = [];
    ebUrl: HTMLInputElement = null;

    onLoad(): void {
        this.ebUrl.value = data.storage.get("client-url", this.ebUrl.value);
        this.onClickAddClient();
    }

    onClickAddClient() {
        let client = Prefab.Instantiate(Client);
        this.addChild(client, this.clientWrap);
        client.lbTitle.innerText = `客户端${Random.range(1000, 9999)}`;
        client.subject.on("dispose", this.onClientDispose, this);
        if (this.ebUrl.value) {
            client.src = this.ebUrl.value;
        }
        else {
            client.src = "index-single.html";
        }
        this.clients.push(client);
    }
    onClientDispose(client: Client) {
        let ind = this.clients.findIndex(ele => ele === client);
        if (ind !== -1) {
            this.clients.splice(ind, 1);
        }
    }
    onChangeColumn() {
        if (this.clientWrap.hasAttribute("row")) {
            this.clientWrap.removeAttribute("row");
        }
        else {
            this.clientWrap.setAttribute("row", "");
        }
    }

    onEditUrl() {
        data.storage.set("client-url", this.ebUrl.value);
    }

    onClickSettings() {
        Utils.app.root.addChild(Prefab.Instantiate(BoxSettings));
    }
};